/**
 * Copyright (c) Software Institute of PKU
 * All rights reserved. 
 * This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 */
package edu.pku.sei.modeler.editparts;

import java.beans.PropertyChangeEvent;
import java.util.List;

import org.eclipse.draw2d.Figure;
import org.eclipse.draw2d.IFigure;
import org.eclipse.draw2d.Label;
import org.eclipse.gef.editpolicies.DirectEditPolicy;

import edu.pku.sei.modeler.figures.MetaClassFigure;
import edu.pku.sei.modeler.figures.MetaEnumFigure;
import edu.pku.sei.modeler.icons.ModelerUIIcons;
import edu.pku.sei.modeler.model.core.MetaAttribute;
import edu.pku.sei.modeler.model.core.MetaClass;
import edu.pku.sei.modeler.model.core.MetaEnum;
import edu.pku.sei.modeler.model.core.MetaEnumLiteral;
import edu.pku.sei.modeler.model.structure.MetaNode;
import edu.pku.sei.modeler.model.structure.MetaShapeElement;
import edu.pku.sei.modeler.util.ResourceFactory;

/**
 * @author zl
 *
 */
public class MetaEnumEditPart extends MetaNamedElementEditPart {
	public MetaEnumEditPart(MetaShapeElement model) {
		super(model);
	}

	/* (non-Javadoc)
	 * @see org.eclipse.gef.editparts.AbstractGraphicalEditPart#createFigure()
	 */
	@Override
	protected IFigure createFigure() {
		Label header = new Label();
		header.setFont(ResourceFactory.getHeadFont());
		MetaEnumFigure fig = new MetaEnumFigure(header);
		header.setIcon(ModelerUIIcons.getImage(ModelerUIIcons.ENUM));
		return fig;
	}

	protected void createEditPolicies() {
		super.createEditPolicies();
		
	}
	
	protected DirectEditPolicy createDirectEditPolicy() {
		return super.createDirectEditPolicy();
	}
	
	@Override
	protected IFigure getDirectEditFigure() {
		Label header = (Label)((MetaEnumFigure)getFigure()).getHeader();
		return header;
	}
	
	protected MetaEnum getMetaEnum() {
		return (MetaEnum)((MetaNode)getModel()).getModelElement();
	}
	
	protected void handlePropertyChanged(PropertyChangeEvent event) {
		super.handlePropertyChanged(event);
	}
	
	protected void refreshVisuals() {
		super.refreshVisuals();
		Label header = (Label)((MetaEnumFigure)getFigure()).getHeader();
		header.setText(getMetaEnum().getName());
		
		refreshEnumLiterals(); //add by liuyi 2007-11-23
	}
	
	//add by liuyi 2007-11-23
	protected void refreshEnumLiterals() {
		MetaEnumFigure fig = (MetaEnumFigure)this.getFigure();
		IFigure body = fig.getLiteralCompart();
		MetaEnum model = (MetaEnum)((MetaShapeElement)getModel()).getModelElement();
		List v = model.getEnumLiterals();
		((Figure)body).removeAll();
		for(int i=0; i<v.size(); i++)
		{
			Label attr = getEnumLiteralsFigure((MetaEnumLiteral)v.get(i));
			body.add(attr);
		}
	}
	
	//add by liuyi 2007-11-23
	public Label getEnumLiteralsFigure(MetaEnumLiteral attr) {
		Label fig = new Label();
		String type = "null";
		fig.setText(attr.getName());
//		fig.setIcon(UMLViewImages.getVisibilityImage(attr.getVisibility()));
		return fig;
	}
}
